import 'package:fish_redux/fish_redux.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/studentsReview/entity/studentReviewEntity.dart';

import 'action.dart';
import 'state.dart';

Reducer<studentsReviewState> buildReducer() {
  return asReducer(
    <Object, Reducer<studentsReviewState>>{
      studentsReviewAction.action: _onAction,
      studentsReviewAction.success: _onSuccess,
    },
  );
}

studentsReviewState _onAction(studentsReviewState state, Action action) {
  final studentsReviewState newState = state.clone();
  return newState;
}

studentsReviewState _onSuccess(studentsReviewState state,Action action) {
  final studentsReviewState newState = state.clone();
  List<studentReviewEntity> list = action.payload;
  if (list.length < Constants.PAGE_SIZE) {
    state.easyRefreshController.finishLoad(noMore: true);
  }
  if (newState.page == 1) {
    newState..items = list;
    state.easyRefreshController.finishRefresh();
  } else {
    newState.items.addAll(list);
    state.easyRefreshController.finishLoad(noMore:list.length!=Constants.PAGE_SIZE);
  }
  return newState;
}